define(
    function (require) {
        require('tpl!./list.tpl.html');
        var util = require('common/util');
        var globalData = require('common/GlobalData').getInstance();

        var showid = 1;
        var page = 1;

        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 20,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '设备名称',
                field: 'name' ,
                resizable : true,
                width: 120,
                content: function (item) {
                    // return '<span class="list-link" data-command-args="' + item.id + '" data-command="modify">' + item.name + '</a>';
                    return '<a class="list-link" href="/echarts.html?sbno='+item.sbNo+'"   target="view_window">' + item.name + '</a>';
                }
            },
            {
                title: '设备编号',
                field: 'sbNo' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    if(item.sbNo !== null){
                        var operates = [
                            '<p class="list-tt" title="'
                            + item.sbNo
                            + '" >'+item.sbNo+'</p>',
                        ];
                        return operates.join('');
                    }else {
                        return "-- -- --";
                    }
                }
            },
            {
                title: '设备规格',
                field: 'sbFormat' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.sbFormat;
                }
            },
            {
                title: '设备价格',
                field: 'price' ,
                width: 100,
                content: function (item) {
                    return monydate(item.price);
                }
            },
            {
                title: '报告价格',
                field: 'reportPrice' ,
                width: 100,
                content: function (item) {
                    return monydate(item.reportPrice);
                }
            },
            {
                title: '检测数量',
                field: 'orderCount' ,
                width: 80,
                content: function (item) {
                    return item.orderCount;
                }

            },
            {
                title: '添加时间',
                field: 'addtime' ,
                width: 140,
                content: function (item) {
                    return textdate(item.addtime);
                }
            },
            {
                title: '存放名称',
                field: 'addressname' ,
                width: 100,
                content: function (item) {
                    return item.addressname;
                }
            },
            {
                title: '当前位置',
                field: 'nowaddress' ,
                width: 100,
                content: function (item) {
                    return buld(item.nowaddress);
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 100,
                content: function (item) {
                    var operates = [
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="modify">修改</a></span>',
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="delete">删除</a></span>'
                    ];
                    return operates.join('');
                }
            }
        ];
        //时间戳转换
        function  textdate(obj) {
            var date = new Date(obj);
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var minuts = date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes();
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }
        //money格式
        function monydate(obj) {
            var money=parseFloat(obj).toLocaleString()
          return money;
        }
        //判断是否投放
        function buld(obj) {
            if(obj !== null){
               return obj;
            }else{
                return "暂未投放";
            }
        }

        function search() {
            var sbno = this.get('sbno');
            page = 1;
            if (sbno.getValue().length > 0) {
                this.fire('search', { sbno: sbno.getValue() });
            } else {
                alert("设备编号不能为空！");
            }
        }

        //分页
        function flip(e) {
            var page = e.target.getAttribute('data-page');
            if (/(\+|\-)/.test(page)) {
                page = parseInt(page) + (RegExp.$1 === '+' ? 1 : -1);
            }
            this.fire('flip', { page: page});
        }

        var BaseListView = require('common/ListView');

        function EquipmentListView() {
            BaseListView.apply(this, arguments);
        }

        EquipmentListView.prototype.template = 'equipmentListPage';

        EquipmentListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);
            var str = location.href;
            var num = str.indexOf("=");
            if(num === -1){
                page = 1;
            }else {
                var name = str.substring(num-4, num);
                if(name === 'page'){
                    page = str.substring(num + 1, str.length);
                }else {
                    page = 1;
                }
            }
            showid = (page-1)*10+1;
            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无设备信息!</p>'
                }
            };
        }

        EquipmentListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );

            this.get('searchButton').on(
                'click',
                util.bind(search, this)
            );

            $('#list-page').on(
                'click',
                ':not(.disable)' ,
                util.bind(flip, this)
            );

            this.get('list').initChildren();
        }

        require('er/util').inherits(EquipmentListView, BaseListView);
        return EquipmentListView;
    }
);
